<?php

namespace App\Repository\Remark;

use App\Models\Remark\OrderRemarkModel as model;
use App\Repository\BaseRepository;
use Illuminate\Support\Facades\DB;

class OrderRemarkRepository extends BaseRepository
{
    public $model;

    public function __construct(model $model)
    {
        $this->model = $model;
        parent::__construct();
    }

    public function index($request)
    {
        // $where['like'] = ['name', 'phone', 'company_name'];
        // $cron['like'] = $this->getWhere($where['like'], $request);
        if (!empty($request['dateRange']) && !empty($request['dateRange'])) {
            $cron['between']['order_remark.created_at'][] = $request['dateRange'][0];
            $cron['between']['order_remark.created_at'][] = $request['dateRange'][1];
        }
        if (isset($request['orderno']) && !empty(($request['orderno']))) {
            $cron['where']['orders.ordersno'] = $request['orderno'];
        }
        if (isset($request['contact_name']) && !empty(($request['contact_name']))) {
            $cron['where']['orders_user.contact_name'] = $request['contact_name'];
        }
        if (isset($request['express_number']) && !empty(($request['express_number']))) {
            $cron['where']['orders.express_number'] = $request['express_number'];
        }
        if (isset($request['contact_phone']) && !empty(($request['contact_phone']))) {
            $cron['where']['orders_user.contact_phone'] = $request['contact_phone'];
        }
        if (isset($request['dist_orderno']) && !empty(($request['dist_orderno']))) {
            $cron['where']['orders_user.dist_orderno'] = $request['dist_orderno'];
        }
        if (isset($request['sup_orderno']) && !empty(($request['sup_orderno']))) {
            $cron['where']['orders_user.sup_orderno'] = $request['sup_orderno'];
        }
        if (isset($request['pay_identity']) && !empty(($request['pay_identity']))) {
            $cron['where']['orders_user.pay_identity'] = $request['pay_identity'];
        }
        if (isset($request['status']) && !empty(($request['status']))) {
            $cron['where']['order_remark.status'] = $request['status'];
        }
        if (isset($request['user_id']) && !empty(($request['user_id']))) {
            $cron['where']['order_remark.user_id'] = $request['user_id'];
        }
//        $cron = [];
        // var_dump($cron);
        $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'order_remark.id';
        $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
        $pageNums = 0;
        $cron['limit']['number'] = isset($request['pageSize']) ? ($request['pageSize']) : 10;
        if (isset($request['pageNum']) && !empty(($request['pageNum']))) {
            $pageNums = ($request['pageNum'] - 1) * $cron['limit']['number'];
        }
        $cron['limit']['start'] = $pageNums;
        // $cron['where']['order_remark.status'] = 0;
        $cron['join'][] = ['table' => 'orders', 'left' => 'ordersno', 'right' => ('order_remark.order_num')];
        $cron['join'][] = ['table' => 'orders_user', 'left' => 'id', 'right' => ('orders.orders_user_id')];
        $cron['select'] = [('orders.*'), 'orders_user.*', 'order_remark.remark as remarkss', 'order_remark.id as remarkId', 'order_remark.user_name'];
        $cron['where']['order_remark.shop_id'] = $request['user']['shop_id'];
        if (!empty($request->get('params'))) {
            $time = $request->get('params');
            $where['between'] = ['order_remark.created_at' => [$time['beginTime'], $time['endTime']]];
        }

        // DB::connection()->enableQueryLog();
        $data = $this->search($this->model, $cron, 1, 10);
        // $log = DB::getQueryLog();
        // var_dump($log);
        if (!empty($data['data'])) {
            foreach ($data['data'] as $key => &$val) {
                if (!empty($val['channel_error_josn'])) {
                    $val['channel_error_josn'] = json_decode($val['channel_error_josn'], true);
                }
            }
        }
        return $data;
    }

    //添加
    public function add($data)
    {
        //必填 字段
        $mustField = [
            'order_num',
            'user_id'
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'order_num' => '订单编号不能为空',
            'user_id' => '指定员工不能为空'
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        // 判断是否存在 如果存在 则将其修改成流转
        // $this->model->where(['id'=>''])->update();
        $userArr = DB::table('admin_users')->where(['id' => $insertData['user_id']])->first();
        if (!$userArr) {
            return $this->returnMsg(100, '未查询到员工信息');
        }
        $insertData['user_name'] = $userArr->user_name;
        $insertData['shop_id'] = $data->user->shop_id;
        $insertData['created_at'] = date('Y-m-d H:i:s');
        $insertData['status'] = 0;
        $insertData['created_by_name'] = $data->user->user_name;
        $insertData['created_by'] = $data->user->id;
        // 查询是否存在 如果存在 直接流转
        // 需要修改 抖音里面的状态
        $isTrue = $this->getOne(['order_num' => $insertData['order_num']]);
        if (!$isTrue) {
            DB::table('order')->where(['id' => $data['ids']])->update(['is_sign' => 1]);
        }
        if ($isTrue) {
            DB::table('order_remark')->where(['order_num' => $insertData['order_num']])->update(['status' => 1]);
        }

        $res = $this->model->insert($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功');
        }
        return $this->returnMsg(1, '添加失败');
    }

    //删除
    public function del($where)
    {
        $flag = true;
        $msg = '未知错误';
        $code = 1;
        if (empty($where)) {
            $flag = false;
            $msg = '删除条件不能为空';
            $code = 2;
        }
        if ($flag) {
            $res = $this->model->where($where)->get();
            if (!$res) {
                $flag = false;
                $msg = '未查询到数据';
                $code = 3;
            }
        }
        if ($flag) {
            $del = $this->model->where($where)->delete();
            if ($del) {
                $msg = '删除成功';
                $code = 200;
            }
        }
        return $this->returnMsg($code, $msg);
    }

    //修改
    public function edit($where, $data, $isMust = false)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        //过滤字段
        $optionalFields = $this->model->getTableField();
        $field = $this->getRealData($data, [], $optionalFields, []);
        $isTrue = $this->model->where($where)->first();
        if (!$isTrue) {
            return $this->returnMsg(100, '未查询到修改的数据');
        }
        //先查询是否存在
        $res = $this->model->where($where)->update($field);
        if ($isMust && $res) {
            if ($field['status'] == 2) {
                // $updateData = ['is_sign' => 0];
                // DB::table('douyin_catch')->where(['dist_orderno' => $isTrue->order_num])->update(['is_sign' => 0]);
            }

            return $this->returnMsg(200, '修改成功');
        }
        if (!$isMust) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    //查看  存入缓存
    public function sel($where = [], $page = 1, $limit = 10)
    {
        $res = $this->search($this->model, $where, $page, $limit);
        return $res;
    }

    //查询 单条数据
    public function oneSel($where)
    {
        return $this->model->where($where)->first();
    }

    public function getOne($where, $field = ['*'])
    {
        return $this->model->where($where)->select($field)->orderBy('id', 'desc')->first();
    }
}
